package com.delong.service.market;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.delong.dao.DaoHelper;
import com.delong.dao.market.ScheduleContentMapper;
import com.delong.model.market.ScheduleContent;

public class ScheduleContentService {

	public void addElement(ScheduleContent element) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			ScheduleContentMapper mapper = session.getMapper(ScheduleContentMapper.class);
			mapper.addElement(element);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public void updateElement(ScheduleContent element) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			ScheduleContentMapper mapper = session.getMapper(ScheduleContentMapper.class);
			mapper.changeElement(element);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}
	
	public void changeStatus(int id) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			ScheduleContentMapper mapper = session.getMapper(ScheduleContentMapper.class);
			mapper.changeStatus(id);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public int getListCount(int schedule_id) {
		SqlSession session = null;
		int result = 0;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			ScheduleContentMapper mapper = session.getMapper(ScheduleContentMapper.class);
			result = mapper.selectListcount(schedule_id);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}

	public List<ScheduleContent> getListElement(int schedule_id, int pageno, int limit) {
		SqlSession session = null;
		List<ScheduleContent> result = new ArrayList<ScheduleContent>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			ScheduleContentMapper mapper = session.getMapper(ScheduleContentMapper.class);
			result = mapper.selectList(schedule_id, pageno, limit);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}

	public ScheduleContent getElement(int id) {
		SqlSession session = null;
		ScheduleContent result = new ScheduleContent();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			ScheduleContentMapper mapper = session.getMapper(ScheduleContentMapper.class);
			result = mapper.selectElementById(id);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}
	
	
	public List<Integer> selectElementByContent (String content) {
		SqlSession session = null;
		List<Integer> result = new ArrayList<Integer>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			ScheduleContentMapper mapper = session.getMapper(ScheduleContentMapper.class);
			result = mapper.selectElementByContent(content);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}

}
